﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Business.Modules.Configuation;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;
using DMS.BaseClass;

namespace DMS.Web.Common.Controls
{
    public partial class ConfigSettingForDeptCode : ContentFormControlBase, IConfigSetting
    {
        #region 成员属性
        public TextBoxMode ValueBoxTextMode
        {
            get
            {
                return this.txtConfigValue.TextMode;
            }
            set
            {
                this.txtConfigValue.TextMode = value;
            }
        }
        public int ValueBoxRows
        {
            get
            {
                return this.txtConfigValue.Rows;
            }
            set
            {
                this.txtConfigValue.Rows = value;
            }
        }
        public Unit ValueBoxWidth
        {
            get
            {
                return this.txtConfigValue.Width;
            }
            set
            {
                this.txtConfigValue.Width = value;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region IConfigSetting Members
        public string ConfigID
        {
            get
            {
                return this.hidConfigID.Value;
            }
            set
            {
                this.hidConfigID.Value = value;
            }
        }

        public string MemberID
        {
            get
            {
                return this.hidMemberID.Value;
            }
            set
            {
                this.hidMemberID.Value = value;
            }
        }

        public string MemberType
        {
            get
            {
                return this.hidMemberType.Value;
            }
            set
            {
                this.hidMemberType.Value = value;
            }
        }

        public void InitForm()
        {
            if (string.IsNullOrEmpty(this.MemberID))
            {
                this.InitSysConfigForm();
            }
            else
            {
                this.InitMemberConfigForm();
            }
        }

        public void Save()
        {
            if (string.IsNullOrEmpty(this.MemberID))
            {
                this.SaveSysConfigForm();
            }
            else
            {
                this.SaveMemberconfigForm();
            }
        }

        #endregion

        #region 界面逻辑
        private void InitSysConfigForm()
        {
            SysConfig config = ConfigHelper.GetSysConfigByID(this.ConfigID);
            if (config != null)
            {
                this.lblConfigTitle.Text = config.Name;
                this.txtConfigValue.Text = config.Value;
            }
        }
        private void InitMemberConfigForm()
        {
            SysConfig defaultConfig = null;
            MemberConfig config = ConfigHelper.GetMemberConfig(MemberID, this.ConfigID);
            if (config != null)
            {
                defaultConfig = config.Default;
                this.lblConfigTitle.Text = config.Name;
                this.txtConfigValue.Text = config.Value;
            }
            else
            {
                defaultConfig = ConfigHelper.GetSysConfigByID(this.ConfigID);
                if (defaultConfig != null)
                {
                    this.lblConfigTitle.Text = defaultConfig.Name;
                    this.txtConfigValue.Text = defaultConfig.Value;
                }
                else
                {
                    ClientHelper.Instance.Alert("读取默认配置项时出错，请联系管理员。");
                }
            }
        }
        private void SaveSysConfigForm()
        {
            SysConfig config = ConfigHelper.GetSysConfigByID(this.ConfigID);
            config.Value = this.txtConfigValue.Text;

            config.Save();
        }
        private void SaveMemberconfigForm()
        {

            List<MemberConfig> memberConfigs = ConfigHelper.GetMemberConfigs(MemberID);
            MemberConfig config = (from c in memberConfigs
                                   where c.ID == this.ConfigID
                                   select c).ToList().DefaultIfEmpty(null).FirstOrDefault();
            if (config == null)
            {
                Department dept = UserHelper.GetDepartmentByID(MemberID);
                SysConfig defaultConfig = ConfigHelper.GetSysConfigByID(this.ConfigID);

                config = new MemberConfig(dept, defaultConfig);
            }

            config.Value = this.txtConfigValue.Text;
            config.Save();
        }
        #endregion
    }
}